




import ply.lex






class Comp(object):

##    def __init__(self):
##        """ Create a new Lexer.
##        
##            error_func:
##                An error function. Will be called with an error
##                message, line and column as arguments, in case of 
##                an error during lexing.
##                
##            type_lookup_func:
##                A type lookup function. Given a string, it must
##                return True IFF this string is a name of a type
##                that was defined with a typedef earlier.
##        """
##
##        # Allow either "# line" or "# <num>" to support GCC's
##        # cpp output
##        #
##        self.line_pattern = re.compile('([ \t]*line\W)|([ \t]*\d+)')

 

    def build1(self):
        """ Builds the lexer from the specification. Must be
            called after the lexer object is created. 
            
            This method exists separately, because the PLY
            manual warns against calling lex.lex inside
            __init__
        """
        self.lexer = ply.lex.lex(object=self)#, **kwargs)

##    def reset_lineno(self):
##        """ Resets the internal line number counter of the lexer.
##        """
##        self.lexer.lineno = 1

    def input(self, text):
        self.lexer.input(text)
    
    def token(self):
        g = self.lexer.token()
        return g


    tokens = ('NUMBER','PLUS','MINUS','TIMES','DIVIDE','LPAREN','RPAREN', )
    t_PLUS    = r'\+'
    t_MINUS   = r'-'
    t_TIMES   = r'\*'
    t_DIVIDE  = r'/'
    t_LPAREN  = r'\('
    t_RPAREN  = r'\)'
    t_ignore  = ' \t'
    
    def t_NUMBER(self,t):
        r'\d+'
        t.value = int(t.value)    
        return t


    # Define a rule so we can track line numbers
    def t_newline(self,t):
        r'\n+'
        t.lexer.lineno += len(t.value)

    def t_COMMENT(self,t):
        r'\//.*'
        
        pass
        # No return value. Token discarded


    # A string containing ignored characters (spaces and tabs)


    # Error handling rule
    def t_error(self,t):
        print "Illegal character '%s'" % t.value[0]
        t.lexer.skip(1)
    # A regular expression rule with some action code

    def find_column(self,input,token):
        last_cr = input.rfind('\n',0,token.lexpos)
        if last_cr < 0:
            last_cr = 0
        column = (token.lexpos - last_cr) + 1
        return column
    

    # Build the lexer
    def principal(self):

    
        self.lexer = lex.lex()

        l=['''1+3+4''','''+++++-------''','''#1247784''']
        for v in l:
            data = v
            self.lexer.input(data)
            while True:
                tok = self.lexer.token()
                if not tok: break      # No more input
                print tok

if __name__ == "__main__":
    filename = '../zp.c.txt'
    text = open('pu.txt').read()
    clex = Comp()
    clex.build1()
    
    #text='''1+3+4'''
    clex.input(text)

    while 1:
        tok = clex.token()
        if not tok: break
            
        #~ print type(tok)
        print "-", tok

    
